package it.blueocean.acanto.taglibrary.jsp.taglib.navigation;

import it.blueocean.acanto.entities.platform.MenuItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class MenuList extends ArrayList<MenuItem>
{
	private static final long serialVersionUID = 1L;

	public String getUrl(String paramString)
	{
		MenuItem localMenuItem = getMenuItem(paramString);
		if(localMenuItem != null)
			return localMenuItem.getUrl();
		return null;
	}

	public List<MenuItem> getSublist(String paramString)
	{
		ArrayList<MenuItem> localArrayList = new ArrayList<MenuItem>();
		recurse(getMenuItem(paramString), localArrayList);
		Iterator<MenuItem> localIterator = localArrayList.iterator();
		while (localIterator.hasNext())
		{
			MenuItem localMenuItem = localIterator.next();
			System.out.println("Menu item sublist : " + localMenuItem.getId());
		}
		return localArrayList;
	}

	protected void recurse(MenuItem paramMenuItem, List<MenuItem> paramList)
	{
		Iterator<MenuItem> localIterator = paramMenuItem.getChildren().iterator();
		while (localIterator.hasNext())
			recurse(localIterator.next(), paramList);
		paramList.add(paramMenuItem);
	}

	public void buildTree()
	{
		Iterator<MenuItem> localIterator1 = iterator();
		MenuItem localMenuItem1;
		
		while (localIterator1.hasNext())
		{
			localMenuItem1 = localIterator1.next();
			Iterator<MenuItem> localIterator2 = iterator();
			
			while (localIterator2.hasNext())
			{
				MenuItem localMenuItem2 = localIterator2.next();
				if(localMenuItem2.getId().equals(localMenuItem1.getParent()))
				{
					localMenuItem1.setParentRef(localMenuItem2);
					localMenuItem2.getChildren().add(localMenuItem1);
				}
			}
		}
		
		localIterator1 = iterator();
		
		while (localIterator1.hasNext())
		{
			localMenuItem1 = (MenuItem) localIterator1.next();
			Collections.sort(localMenuItem1.getChildren());
		}
		
		setSelected("/");
	}

	public MenuItem getMenuItem(String paramString)
	{
		Iterator<MenuItem> localIterator = iterator();
		while (localIterator.hasNext())
		{
			MenuItem localMenuItem = localIterator.next();
			if(localMenuItem.getId().equals(paramString))
				return localMenuItem;
		}
		return null;
	}

	public void setSelected(String s)
	{
		MenuItem menuitem1;
		for (@SuppressWarnings("rawtypes")
		Iterator iterator = iterator(); iterator.hasNext(); menuitem1.setSelected(false))
			menuitem1 = (MenuItem) iterator.next();
		MenuItem menuitem = getMenuItem(s);
		MenuItem menuitem2 = menuitem.getChild(0);
		if(menuitem2 != null)
			menuitem = menuitem2;
		for (; menuitem != null; menuitem = menuitem.getParentRef())
			menuitem.setSelected(true);
	}
	
	public String[] getExpandedPath(String paramString, boolean paramBoolean)
	{
		ArrayList<String> localArrayList = new ArrayList<String>();
		MenuItem localMenuItem1 = getMenuItem(paramString);
		for (MenuItem localMenuItem2 = localMenuItem1; localMenuItem2 != null; localMenuItem2 = localMenuItem2.getParentRef())
			localArrayList.add(0, localMenuItem2.getId());
		if(paramBoolean)
			for (MenuItem localMenuItem3 = (MenuItem) localMenuItem1.getChildren().get(0); localMenuItem3 != null; localMenuItem3 = null)
				while (true)
				{
					localArrayList.add(localMenuItem3.getId());
					if(localMenuItem3.getChildren().size() <= 0)
						break;
					localMenuItem3 = (MenuItem) localMenuItem3.getChildren().get(0);
				}
		int i = 0;
		String[] arrayOfString = new String[localArrayList.size()];
		Iterator<String> localIterator = localArrayList.iterator();
		while (localIterator.hasNext())
		{
			String str = localIterator.next();
			arrayOfString[(i++)] = str;
		}
		return arrayOfString;
	}
}
